package park.another_prospect;

import java.util.Random;
import java.util.Timer;
import java.util.TimerTask;


public class Car
{
    private static Timer exitTimer = new Timer(true);
    private final Random random = new Random();

    private final String licensePlate;
    private final int parkTimeInSeconds = random.nextInt(10000) + 10; // 10s - 10010s

    public Car(String licensePlate)
    {
        this.licensePlate = licensePlate;
    }

    public void enter(final ParkingLot parkingLot)
    {
        parkingLot.park(this);
        exitTimer.schedule(new TimerTask()
        {
            @Override
            public void run()
            {
                parkingLot.exit(licensePlate);
            }
        }, (long) parkTimeInSeconds * 1000);
    }

    public void park(ParkingLot parkingLot)
    {
        parkingLot.enter(licensePlate);
    }

    @Override
    public String toString()
    {
        return "Car{" +
                "licensePlate='" + licensePlate + '\'' +
                '}';
    }
}
